package com.example.springboot.service.imp;

import com.example.springboot.bean.Employee;
import com.example.springboot.repository.EmployeeRepository;
import com.example.springboot.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableArgumentResolver;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by tang on 2017/11/21.
 */
@Service
public class EmployeeServiceImp implements EmployeeService {

    @Resource
    private EmployeeRepository employeeRepository;


    @Resource
    private JdbcTemplate jdbcTemplate;

    @Override
    public Employee findByName(String name) {
        return employeeRepository.findByName(name);
    }


    public Employee findByName2(String name) {
        RowMapper<Employee> rowMapper = new BeanPropertyRowMapper(Employee.class);
        Employee employee = jdbcTemplate.queryForObject("select * from employee where name=?", new Object[]{name}, rowMapper);
        return employee;

    }

    @Override
    public Employee findById(int id) {
        return employeeRepository.findById(id);
    }

    @Override
    public void save(Employee employee) {
        employeeRepository.save(employee);
    }

    @Override
    public void delete(int id) {
        employeeRepository.delete(id);

    }

    @Override
    public void update(Employee employee) {
        employeeRepository.save(employee);

    }



    @Override
    public Iterable<Employee> findAll(Pageable pageable) {
        return employeeRepository.findAll(pageable);
    }

    @Override
    public long count() {
        return  employeeRepository.count();
    }
}
